package top.raoqq.sky.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import top.raoqq.sky.entity.sql.Role;
import top.raoqq.sky.enums.SkyResponseEnum;
import top.raoqq.sky.exception.BizException;
import top.raoqq.sky.service.RoleService;
import top.raoqq.sky.vo.Result;
import top.raoqq.sky.vo.RoleQueryVO;
import javax.validation.Valid;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 饶奇奇
 * @date 2023/8/14
 */
@RestController
@Api(tags = "角色管理")
@RequestMapping("/role")
@RequiredArgsConstructor
public class RoleController {

    /**
     * 获取所有的角色ID
     */
    private static final String LIST_ROLE_ID = "list";

    /**
     * 分页条件查询
     */
    private static final String LIST_ROLE_PAGE_CONDITION = "listRolePageCondition/{pageNumber}/{pageSize}";

    /**
     *  新增或者修改角色
     */
    private static final String SAVE_OR_UPDATE_ROLE = "saveOrUpdateRole";

    /**
     * 根据主键删除角色
     */
    private static final String REMOVE_ROLE_BY_ID = "removeRoleById/{roleId}";

    private final RoleService roleService;

    @GetMapping(LIST_ROLE_ID)
    public Result<List<Map<String, Object>>> listRoleId() {
        return Result.success(roleService.listRoleId());
    }

    @GetMapping(LIST_ROLE_PAGE_CONDITION)
    public Result<Page<Role>> listRolePageCondition(@PathVariable Long pageNumber,
                                                    @PathVariable Long pageSize,
                                                    @Valid RoleQueryVO roleQueryVO) {
        LocalDate startTime = roleQueryVO.getStartTime();
        LocalDate endTime = roleQueryVO.getEndTime();
        if (Objects.nonNull(startTime) && Objects.nonNull(endTime)) {
            // 开始时间大于结束时间
            if (startTime.compareTo(endTime) > 0) {
                throw new BizException(SkyResponseEnum.END_TIME_NOT_CAN_LT_START_TIME);
            }
        }
        Page<Role> page = new Page<>(pageNumber, pageSize);
        roleService.listRolePageCondition(page,
                roleQueryVO.getRoleName(),
                roleQueryVO.getRoleKey(),
                roleQueryVO.getStartTime(),
                roleQueryVO.getEndTime());
        return Result.success(page);
    }


    @PostMapping(SAVE_OR_UPDATE_ROLE)
    public Result<Void> saveOrUpdateRole(@RequestBody @Valid Role role) {
        // 参数校验
        roleService.checkSaveOrUpdateArguments(role);
        // 保存或者修改数据
        roleService.saveOrUpdateRole(role);
        return Result.success();
    }

    @DeleteMapping(REMOVE_ROLE_BY_ID)
    public Result<Void> removeRoleById(@PathVariable Long roleId) {
        // 参数校验
        if (roleId <= 0) {
            throw new BizException(SkyResponseEnum.ARGUMENTS_ERROR);
        }
        roleService.removeById(roleId);
        // 删除角色绑定的菜单
        roleService.unBindMenu(roleId);
        return Result.success();
    }

}
